﻿using UnityEngine;
using System.Collections;

public class TabletGameInput : MonoBehaviour {

    private BuildingData selectedBuildingData = null;
    private Rect buildingWindowRect;
    private bool closeGame = false;

	// Use this for initialization
	void Start () {
	
	}
	
	// Update is called once per frame
	void Update () {

        if (Input.GetKeyDown(KeyCode.Escape))
        {
            Application.Quit();
        }

        if (selectedBuildingData == null && Input.GetMouseButtonDown(0))
        {
            print("A touch was received");
            RaycastHit hitInfo;
            if (Physics.Raycast(Camera.main.ScreenPointToRay(Input.mousePosition), out hitInfo))
            {
                print("Hit an object tagged " + hitInfo.collider.tag + " named " + hitInfo.collider.name);
                if (hitInfo.collider.tag == "Building")
                {
                    print("The touch hit a building");
                    selectedBuildingData = hitInfo.collider.GetComponent<BuildingData>();
                    buildingWindowRect = new Rect(Screen.width * 0.05f, Screen.height * 0.05f, Screen.width * 0.9f, Screen.height * 0.9f);
                }
            }
            else
            {
                print("It hit nothing");
            }
        }
	}

    void OnGUI()
    {
        if (selectedBuildingData != null)
        {
            print("Totally showing the window");
            buildingWindowRect = GUI.Window(0, buildingWindowRect, DoBuildingDataWindow, selectedBuildingData.buildingName);
        }
    }

    public void DoBuildingDataWindow(int windowId)
    {
        GUILayout.BeginVertical();

        GUILayout.Label("TODO: Add data about the building inside this window");

        if (GUILayout.Button("Close Window"))
        {
            selectedBuildingData = null;
        }

        GUILayout.EndVertical();
    }
}
